﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;

namespace LitTools
{
    /// <summary>
    /// 声音播放器查看器
    /// 可以查看当前的播放器信息
    /// </summary>
    public class AudioPlayerDebugger : EditorWindow
    {
        GUIStyle tipStyle = new GUIStyle();
        Vector2 trackListScroll = Vector2.zero;
        List<bool> trackFoldouts = new List<bool>();
        List<bool> playerFoldouts = new List<bool>();
        [MenuItem("LitTools/LitAudio/AudioPlayerDebugger")]
        public static void ShowWindow()
        {
            var window = GetWindow<AudioPlayerDebugger>();
            window.titleContent = new GUIContent("AudioPlayerDebuger");
            window.Show();
        }

        private void OnEnable()
        {
            tipStyle.fontSize = 20;
            tipStyle.fontStyle = FontStyle.Bold;
            tipStyle.normal.textColor = Color.white;
            tipStyle.alignment = TextAnchor.MiddleCenter;
            tipStyle.contentOffset = new Vector2(0, 20);
        }

        private void OnGUI()
        {
            //当前场景中的音轨列表
            if (!Application.isPlaying)
            {
                GUILayout.Label("请在运行状态查看", tipStyle);
                return;
            }
            //展示运行状态信息
            trackListScroll = GUILayout.BeginScrollView(trackListScroll);
            ShowTrackList();
            GUILayout.EndScrollView();
        }

        /// <summary>
        /// 显示音轨列表
        /// </summary>
        private void ShowTrackList()
        {
            var trackList = AudioManager.Instance.TrackDict.Values.ToList();
            while (trackFoldouts.Count <= trackList.Count)
                trackFoldouts.Add(false);
            for (int i = 0; i < trackList.Count; i++)
            {
                trackFoldouts[i] = EditorGUILayout.Foldout(trackFoldouts[i], trackList[i].Id);
                if (trackFoldouts[i])
                {
                    ShowTrackInfo(trackList[i]);
                }
            }
        }
        /// <summary>
        /// 显示音轨信息
        /// </summary>
        /// <param name="trackInfo"></param>
        private void ShowTrackInfo(AudioTrackInfo trackInfo)
        {
            GUILayout.BeginVertical("box");
            GUILayout.Label($"trackId:{trackInfo.Id}");
            GUILayout.Label($"trackVolume:{trackInfo.volume}");
            ShowPlayerList(trackInfo);
            GUILayout.EndVertical();
        }

        /// <summary>
        /// 显示播放器列表
        /// </summary>
        /// <param name="trackInfo"></param>
        private void ShowPlayerList(AudioTrackInfo trackInfo)
        {
            while (playerFoldouts.Count < AudioManager.Instance.PlayerList.Count)
                playerFoldouts.Add(false);
            var playerList = AudioManager.Instance.PlayerList;
            for (int i = 0; i < playerList.Count; i++)
            {
                if (playerList[i].Track != trackInfo)
                    continue;
                playerFoldouts[i] = EditorGUILayout.Foldout(playerFoldouts[i], playerList[i].name);
                if (playerFoldouts[i])
                {
                    ShowPlayerInfo(playerList[i]);
                }
            }
        }

        /// <summary>
        /// 显示播放器信息
        /// </summary>
        /// <param name="player"></param>
        private void ShowPlayerInfo(AudioPlayer player)
        {
            GUILayout.BeginVertical("box");
            Rect rect = GUILayoutUtility.GetRect(18, 18, "TextField");
            EditorGUI.ProgressBar(rect, 0.5f,"Progress");
            GUILayout.EndVertical();
        }
    }
}
